package com.cn.wanxi.servlet.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.MyImg;
import com.cn.wanxi.util.MyTool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/back/product/update")
public class ProductUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        MyImg.upload(req);
        ProductModel productModel = new ProductModel();
        productModel.setName((String) req.getAttribute("productName"));
        productModel.setNormalPrice(MyTool.parseDouble(req.getAttribute("normalPrice")));
        productModel.setPrice(MyTool.parseDouble(req.getAttribute("price")));
        productModel.setCuisineId(MyTool.parseInt(req.getAttribute("cuisine")));
        String imghref = (String) req.getAttribute("imgHref");

        productModel.setImgHref(imghref.equals("undefined") ? "" : imghref);
        productModel.setIsShow(MyTool.parseInt(req.getAttribute("isShow")));
        productModel.setIsRecommend(MyTool.parseInt(req.getAttribute("isRecommend")));
        productModel.setId(MyTool.parseInt(req.getAttribute("productId")));
        productModel.setContent((String) req.getAttribute("content"));

        IProductService iProductService = new ProductServiceImpl();
        String result = iProductService.update(productModel);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);
    }
    /**
     * 1.下载wangEditor的js包
     * 2.定义一个div 设置一个id
     * 3.一定要将创建editor的js写在div之后
     * 4.创建后台服务器图片的servlet   注意：地址editUrl='/loadImg/img'
     * 5.在servlet里面一定要返回一个json对象 且对象内容有url
     *   jsonObject.put("url", req.getAttribute("imgHref"));
     * 6.取得富文本的值editor.txt.html()
     * 7.注意：上传图片时，如果不想更改图片，则后台得到的是undefined，所以，需要判断
     */
}
